import request from '../utils/request'
import type {
  User,
  UserProfile,
  UserPreference,
  UserStats,
  UserUpdate,
  UserPasswordChange,
  UserPreferenceUpdate,
  UserProfileResponse,
  UserResponse,
  UserPreferenceResponse,
  UserStatsResponse,
  ApiResponse
} from '../types/user'

class UserApi {
  // 获取用户个人资料
  async getUserProfile(): Promise<UserProfile> {
    try {
      const data = await request.get('/api/v1/users/profile')
      return data as unknown as UserProfile
    } catch (error) {
      throw new Error('获取用户资料失败')
    }
  }

  // 更新用户个人资料
  async updateUserProfile(data: UserUpdate): Promise<User> {
    try {
      const result = await request.put('/api/v1/users/profile', data)
      return result as unknown as User
    } catch (error) {
      throw new Error('更新用户资料失败')
    }
  }

  // 修改密码
  async changePassword(data: UserPasswordChange): Promise<void> {
    try {
      await request.post('/api/v1/users/change-password', data)
    } catch (error) {
      throw new Error('修改密码失败')
    }
  }

  // 上传头像
  async uploadAvatar(file: File): Promise<string> {
    try {
      const formData = new FormData()
      formData.append('file', file)
      
      const result = await request.post(
        '/api/v1/users/upload-avatar',
        formData,
        {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        }
      )
      
      return (result as any).data.avatar_url as string
    } catch (error) {
      throw new Error('头像上传失败')
    }
  }

  // 获取用户偏好设置
  async getUserPreferences(): Promise<UserPreference> {
    try {
      const data = await request.get('/api/v1/users/preferences')
      return data as unknown as UserPreference
    } catch (error) {
      throw new Error('获取用户偏好失败')
    }
  }

  // 更新用户偏好设置
  async updateUserPreferences(data: UserPreferenceUpdate): Promise<void> {
    try {
      await request.put('/api/v1/users/preferences', data)
    } catch (error) {
      throw new Error('更新用户偏好失败')
    }
  }

  // 添加收藏小说
  async addFavoriteNovel(novelId: number): Promise<void> {
    try {
      await request.post(`/api/v1/users/favorites/novels/${novelId}`)
    } catch (error) {
      throw new Error('添加收藏失败')
    }
  }

  // 移除收藏小说
  async removeFavoriteNovel(novelId: number): Promise<void> {
    try {
      await request.delete(`/api/v1/users/favorites/novels/${novelId}`)
    } catch (error) {
      throw new Error('移除收藏失败')
    }
  }

  // 添加收藏角色
  async addFavoriteCharacter(characterId: number): Promise<void> {
    try {
      await request.post(`/api/v1/users/favorites/characters/${characterId}`)
    } catch (error) {
      throw new Error('添加收藏失败')
    }
  }

  // 移除收藏角色
  async removeFavoriteCharacter(characterId: number): Promise<void> {
    try {
      await request.delete(`/api/v1/users/favorites/characters/${characterId}`)
    } catch (error) {
      throw new Error('移除收藏失败')
    }
  }

  // 获取用户统计信息
  async getUserStats(): Promise<UserStats> {
    try {
      const data = await request.get('/api/v1/users/stats')
      return data as unknown as UserStats
    } catch (error) {
      throw new Error('获取用户统计失败')
    }
  }
}

export const userApi = new UserApi() 